from autoclick import AutoClick
from functools import wraps

class AutuClickJS(AutoClick):
    

    def __init__(self, maximize_window=True, implicitly_wait=60, headless=False,restart=True):
        super().__init__(maximize_window=maximize_window, implicitly_wait=implicitly_wait, headless=headless,restart=restart)
        # js执行脚本
        self.jscommand = ''

    def safe_execute(func):
        @wraps(func)
        def wrap(self,*args,**kwagrs):
            try:
                self.sleep(1)
                try:
                    func(self,*args,**kwagrs)
                except Exception as e:
                    raise Exception('元素未加载')
            except Exception as e:
                print(e)
                wrap(self,*args,**kwagrs)
        return wrap

    # 通过ID查找-点击 
    @safe_execute
    def click(self):
        self.execute("{jscommand}.click()".format(jscommand=self.jscommand))
        print('执行：{jscommand}.click()'.format(jscommand=self.jscommand))

    
        # 通过ID查找-输入 
    @safe_execute
    def input(self,content):
        self.execute("{jscommand}.value = '{content}'".format(jscommand=self.jscommand,content=content))
        print('执行：{jscommand}.click()'.format(jscommand=self.jscommand))

    
    # 通过ClassName选择元素
    def select_by_classname(self,classname,index=0):
        """
        classname:节点的classname
        index: classname对应节点数组中的下表，该方法找的是classname对应的所有节点
        """
        self.jscommand = "document.getElementsByClassName('{classname}')[{index}]".format(classname=classname,index=index)
        return self
    
        # 通过ID选择元素
    def select_by_id(self,id_):
        """
        id_:节点的id_
        """
        self.jscommand = "document.getElementById('{id_}')".format(id_=id_)
        return self
    
        # 通过ID选择元素
    def select_by_name(self,name,index=0):
        """
        classname:节点的classname
        index: classname对应节点数组中的下表，该方法找的是classname对应的所有节点
        """
        self.jscommand = "document.getElementsByName('{name}')[{index}]".format(name=name,index=index)
        return self
    
    def select_by_classname_in_frame(self,name,classname,index=0):
        self.jscommand = "document.getElementsByName('{name}')[0].contentDocument.getElementsByClassName('{classname}')[{index}]".format(name=name,classname=classname,index=index)
        return self
    
    def select_by_name_in_frame(self,name,tagname,index=0):
        self.jscommand = "document.getElementsByName('{name}')[0].contentDocument.getElementsByName('{tagname}')[{index}]".format(name=name,tagname=tagname,index=index)
        return self
    
    def select_by_id_in_frame(self,name,id_):
        self.jscommand = "document.getElementsByName('{name}')[0].contentDocument.getElementById('{id_}')".format(name=name,id_=id_)
        return self
    
    # 选择下一个子节点
    def next(self,index=0):
        """
        index:在父节点中的索引
        """
        try:
            self.jscommand += ".children[{index}]".format(index=index)
            return self
        except Exception as e:
            print('you must call select_by_*() method before use next()')
    